package cn.xk.pet.service.impl;

import cn.xk.basic.service.IBaiduAiAuditService;

import cn.xk.basic.service.impl.BaseServiceImpl;


import cn.xk.basic.util.AjaxResult;
import cn.xk.pet.domain.Pet;
import cn.xk.pet.domain.PetDetail;
import cn.xk.pet.domain.PetOnlineAuditLog;
import cn.xk.pet.mapper.PetDetailMapper;
import cn.xk.pet.mapper.PetMapper;
import cn.xk.pet.mapper.PetOnlineAuditLogMapper;
import cn.xk.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Transactional
    @Override

    public void save(Pet pet) {
        //添加宠物信息
        super.save(pet);
        //添加宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            //设置petDetail的petId
            petDetail.setPetId(pet.getId());
            //添加到数据库
            petDetailMapper.save(petDetail);
        }
    }

    @Transactional
    @Override
    public void del(Long id) {
        //删除宠物详情
        petDetailMapper.delByPetId(id);
        //删除宠物信息
        super.del(id);

    }

    @Transactional
    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            //设置petDetail的petId
            petDetail.setPetId(pet.getId());
            petDetailMapper.updateByPetId(petDetail);
        }
    }

    @Transactional
    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            del(id
            );
        }
    }

    @Transactional
    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());
            if (boo1 && boo2) {
                //审核通过
                //改pet状态：state=1。上架时间
                petMapper.updateOnsaleState(id, new Date());
                //添加审核日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(1);
                log.setNote(id + "审核成功");
                petOnlineAuditLogMapper.save(log);
            } else {
                //添加审核日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(0);
                log.setNote(id + "审核失败");
                petOnlineAuditLogMapper.save(log);
                return new AjaxResult().setSuccess(false).setMsg("上架失败");
            }

        }
        return new AjaxResult();
    }

    @Transactional
    @Override
    public void offsale(List<Long> ids) {
        //改状态state=0,下架时间new Date()
        petMapper.updateOffsaleState(ids, new Date());
    }
}
